package com.gec.wiki.controller;


import com.gec.wiki.entity.Content;
import com.gec.wiki.entity.Doc;
import com.gec.wiki.req.DocQueryReq;
import com.gec.wiki.resp.CommonResp;
import com.gec.wiki.resp.DocQueryResp;
import com.gec.wiki.resp.PageResp;
import com.gec.wiki.service.IContentService;
import com.gec.wiki.service.IDocService;
import com.gec.wiki.utils.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.AbstractDocument;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2023-11-13
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private IDocService docService;
    @Autowired
    private IContentService contentService;

    @RequestMapping("/getDocListByPage")
    public CommonResp getDocListByPage(DocQueryReq req){
        PageResp<DocQueryResp> docListByPage =
                docService.getDocListByPage(req);
        CommonResp<PageResp<DocQueryResp>> listCommonResp =new CommonResp<>();
        listCommonResp.setContent(docListByPage);
        return listCommonResp;
    }
    @RequestMapping("/allList")
    public CommonResp allList(DocQueryReq req){
        List<DocQueryResp> docQueryResps =
                docService.allList(req);
        CommonResp<List<DocQueryResp>> comm = new CommonResp<>();
        comm.setContent(docQueryResps);
        return comm;
    }
    @RequestMapping("/save")
    public CommonResp save(@RequestBody DocQueryReq req){
        Doc doc = CopyUtil.copy(req,Doc.class);
        docService.saveOrUpdate(doc);

        Content content = CopyUtil.copy(req, Content.class);
        contentService.saveOrUpdate(content);

        CommonResp commonResp = new CommonResp();
        return commonResp;
    }
    @GetMapping("/remove")
    public CommonResp remove(Integer [] ids){
        List<Integer> list = Arrays.asList(ids);
        docService.removeByIds(list);
        CommonResp<Object> resp = new CommonResp<>();
        return resp;
    }
    @GetMapping("/findContentById/{id}")
    public CommonResp findContentById(@PathVariable int id){
        Content content = contentService.getById(id);
        CommonResp<String> resp = new CommonResp<>();
        if (content!=null && content.getContent()!=null){
            resp.setContent(content.getContent());
        }
        return resp;
    }
}
